
package com.scs.application.modules.upms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.consts.GlobalConsts;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.upms.entity.Parameter;
import com.scs.application.modules.upms.entity.UserBlock;
import com.scs.application.modules.upms.entity.WorkbenchBlock;
import com.scs.application.modules.upms.mapper.UserBlockMapper;
import com.scs.application.modules.upms.mapper.WorkbenchBlockMapper;
import com.scs.application.modules.upms.request.UserBlockQueryRequest;
import com.scs.application.modules.upms.service.ParameterService;
import com.scs.application.modules.upms.service.UserBlockService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户区块 service 实现
 */
@AllArgsConstructor
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UserBlockServiceImpl extends BaseServiceImpl<UserBlockMapper, UserBlock> implements UserBlockService {

    private final WorkbenchBlockMapper workbenchBlockMapper;

    private final ParameterService parameterService;

    @Override
    public List<UserBlock> list(UserBlockQueryRequest request) {

        List<UserBlock> list = this.list(Wrappers.<UserBlock>query()
                .eq(StringUtils.isNotBlank(request.getLoginKey()), "login_key", request.getLoginKey())
                .orderByAsc("sort")
        );
        return list;
    }

    @Override
    public IPage<UserBlock> page(UserBlockQueryRequest request) {
        IPage page = this.page(request.getPage(), Wrappers.<UserBlock>query()
                .eq(StringUtils.isNotBlank(request.getLoginKey()), "login_key", request.getLoginKey())
                .orderByAsc("sort")
        );
        return page;
    }

    @Override
    public List<UserBlock> listCurrentUserBlock() {
        String loginKey = UserUtils.currentUser().getLoginKey();
        synchronized (loginKey.intern()) {
            try {

                List<UserBlock> blocks = list(new UserBlockQueryRequest().setLoginKey(loginKey));
                if (blocks.size() != 0) {
                    return blocks;
                }
                return restoreBlocks(loginKey);
            } finally {
            }
        }
    }

    @Override
    public List<UserBlock> restoreBlocks(String loginKey) {
        this.remove(Wrappers.<UserBlock>query().eq("login_key", loginKey));

        List<UserBlock> blocks = new ArrayList<>();
        List<WorkbenchBlock> workbenchBlocks = workbenchBlockMapper.listUserBlock(loginKey);

        for (WorkbenchBlock workbenchBlock : workbenchBlocks) {
            UserBlock userBlock = new UserBlock();
            BeanUtils.copyProperties(workbenchBlock, userBlock);
            userBlock.setLoginKey(loginKey).setId(null);
            blocks.add(userBlock);
        }
        this.saveBatch(blocks);

        String paramKey = "project.common.blockInited";
        Parameter parameter = parameterService.getOne(Wrappers.<Parameter>query().eq("owner", loginKey).eq("param_key", paramKey));
        if (parameter == null) {
            parameter = new Parameter();
        }
        parameter.setIsSys(GlobalConsts.YES_STR).setParamKey(paramKey)
                .setOwner(loginKey).setParamName("项目-通用-工作台初始化")
                .setParamValue("1").setRemark("工作台区块已初始化");
        parameterService.saveOrUpdate(parameter);
        return blocks;
    }
}
